package com.ews.onlineexchange.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.ews.onlineexchange.model.ServiceBuyer;

@Stateless
public class ServiceBuyerDao {
	
	@Inject
	EntityManager entityManager;
	
	private static final String FIND_BY_SEQNO_EQL = ""
			+ "	SELECT		ss"
			+ "	FROM		ServiceBuyer ss "
			+ "	WHERE		ss.seqNo = :seqNo	";
	public ServiceBuyer findBySeqNo(String seqNo) {
		Query query = entityManager.createQuery(FIND_BY_SEQNO_EQL);
		query.setParameter("seqNo", seqNo);
		List<ServiceBuyer> resultList = query.getResultList();
		if (resultList.size() == 0)
			return null;
		ServiceBuyer result = resultList.get(0);
		result = getInitializedInstance(result);
		return result;
	}
	
	public ServiceBuyer getInitializedInstance(ServiceBuyer instance) {
		if (!entityManager.contains(instance))
			instance = entityManager.merge(instance);
		instance.getHistoryRejectRecords().size();
		
		return instance;
	}

	public ServiceBuyer find(String id) {		
		return getInitializedInstance(entityManager.find(ServiceBuyer.class, id));
	}
	
}
